var app = getApp();
Page(Object.assign({},{
    data:{
        siteroot : app.siteInfo.siteroot.replace('app/index.php', ''),uniacid: app.siteInfo.uniacid,
        inited : false,
        checkedrule : false,
        agentcode : 0,
        sortname : '',
        sort : [],
        current : 0,
        cityid : 0,
        tradeid : 0,
        sorttwo : '',
		sorttwolist : [],
    },
    onLoad:function(options){
        var self = this;

        var zfid = options.zfid ? options.zfid : '';
        var code = options.scene ? options.scene : (options.agentcode ? options.agentcode : '');
        self.setData({
            options : options,
        })
        
        app.com.auth(function(){
            app.com.http('shopinfo','POST',{op:'joininfo',zfid:zfid},0,false,'','',function(res){
                if (!res.data.errno && res.data) {
                    
                    self.setData({
                        inited : true,
                        status : res.data.data.status,
                        params : res.data.data.params,
                        sharedata : res.data.data.sharedata,
                        sort : res.data.data.sort,
                        city : res.data.data.city,
                        trade : res.data.data.trade,
                        shopbag : res.data.data.shopbag ? res.data.data.shopbag : [],
                        agentcode : code
                    });

                }else{
                    app.com.alert(res.data.message,function(){
                        app.com.backPage(zfid);
                    });
                }
            });
        });
    },
    onShareAppMessage : function(){
        var self = this;
        return {
            title: self.data.sharedata.title,
            path: self.data.sharedata.path,
            imageUrl : self.data.sharedata.img,
        }
    },
    onPullDownRefresh: function () {
        this.onLoad( this.data.options );
        wx.stopPullDownRefresh();
    },
    address(){
        var self = this;
        wx.chooseLocation({
            success : function(res){
                self.setData({
                    lat : res.latitude,
                    lng : res.longitude,
                    address : res.address,
                });
            }
        });
    },
    submit(e){
        var self = this;
        var postdata = {
            op : 'join',
            name : e.detail.value.name,
            shopname : e.detail.value.shopname,
            tel : e.detail.value.phone,
            pass : e.detail.value.pass,
            lat : self.data.lat ? self.data.lat : 0,
            lng : self.data.lng ? self.data.lng : 0,
            address : self.data.address ? self.data.address : '',
            code : e.detail.value.code > 0 ? e.detail.value.code : self.data.agentcode,
            ctaddress : self.data.ctaddress ? self.data.ctaddress : '',
            sortid : self.data.sortid,
            sorttwo : self.data.sorttwo,
            bagid : self.data.shopbag && self.data.shopbag.length > 0 ? self.data.shopbag[self.data.current].id : 0,
            cityid : self.data.cityid,
            tradeid : self.data.tradeid,
        }
        
        if( !postdata.name ) {
            app.com.toast('请填写姓名');return false;
        }
        if( !postdata.tel || !app.com.verify('mobile', postdata.tel) ) {
            app.com.toast('请填写电话');return false;
        }
        if( !postdata.shopname ) {
            app.com.toast('请填写名称');return false;
        }
        if( !postdata.pass ) {
            app.com.toast('请填写登录密码');return false;
        }
        if( postdata.pass.length < 6 || postdata.pass.length > 32 ) {
            app.com.toast('登录密码在6-32位字符之间');return false;
        }
        if( !postdata.lat ) {
            app.com.toast('请选择位置');return false;
        }
        if( !self.data.checkedrule ) {
            app.com.toast('请先同意商户协议');return false;
        }

        app.com.http('shopinfo','POST',postdata,0,true,'','',function(res){
            if( res.data.data.paySign ){
                wx.requestPayment({
					'appId' : res.data.data.appId,
                    'timeStamp': res.data.data.timeStamp,
                    'nonceStr': res.data.data.nonceStr,
                    'package': res.data.data.package,
                    'signType': 'MD5',
                    'paySign': res.data.data.paySign,
                    'success': function (payres) {
                        if( payres.errMsg == 'requestPayment:ok' ){
                            app.com.alert(res.data.data.mess,function(){
                                if( res.data.data.isto == 1 ){
                                    wx.navigateTo({
                                        url : '/zofui_sales/admin/login'
                                    });
                                }
                            });
                            return false;
                        }
                    },
                    'fail': function (payres) {
                        
                    }
                });
            }else{
                app.com.alert(res.data.message,function(){
                    if( res.data.data.isto == 1 ){
                        wx.navigateTo({
                            url : '/zofui_sales/admin/login'
                        });
                    }
                });
                if (!res.data.errno && res.data) {
                    self.setData({
                        status : 1,
                    });
                }
            }
        });
    },
    checkedrule : function(){
        var self = this;
        self.setData({
            checkedrule : !self.data.checkedrule
        })
    },
    bindRegionChange : function(e){
        var self = this;
        self.setData({
            ctaddress : e.detail.value[0] +','+e.detail.value[1] +','+e.detail.value[2]
        })
    },
    selectarea(){
        this.selectComponent('#ausarea').show();
    },
    bindsortChange(e){
        let key = e.detail.value;
        this.setData({
            sortname : this.data.sort[key].name,
            sortid : this.data.sort[key].id,
            sorttwolist : this.data.sort[key].down ? this.data.sort[key].down : [],
            sorttwo : '',
            sorttwoname : '',
        })
    },
    bindsorttwoChange(e){
        let key = e.detail.value;
        this.setData({
            sorttwo : this.data.sorttwolist[key].id,
            sorttwoname : this.data.sorttwolist[key].name,
        })
    },
    bindctyChange(e){
        let key = e.detail.value;
        this.setData({
            cityname : this.data.city[key].name,
            cityid : this.data.city[key].pid,
        })
    },
    bindsqChange(e){
        let key = e.detail.value;
        this.setData({
            tradename : this.data.trade[key].name,
            tradeid : this.data.trade[key].id,
        })
    },
    bindchange(e){
        this.setData({
            current : e.detail
        })
    },
    location : function( e ){
        app.com.location( e );
    },
    otherapp : function( e ){
        app.com.otherapp( e );
    },
    navigateto : function( e ){
        app.com.navigateto( e );
    },
    redirectto : function( e ){
        app.com.redirectto( e );
    },    
    callphone : function(e){
        app.com.callphone( e );
    },
    showimages : function( e ){
        app.com.showimages( e );
    },
    FormIdsubmit : function(e){

    }
}))